
import { v4 as uuidv4 } from 'uuid';
import { userinfo } from '../UserInfo';
import { new_world } from '../../proto/protocol';
import protocol from '../../proto/protoExt';
enum FishMessageType {
    empty = "empty",
    dead = "dead",
    attack_record = "attack_record",
    growth = "growth",
    earn = "earn",
    explore = "explore",
    recover = "recover",
    energy_recover = "energy_recover",
    stay = "stay",
}

function uint8ArrayToBase64(uint8Array: Uint8Array): string {
    let binaryString = '';
    for (let i = 0; i < uint8Array.byteLength; i++) {
        binaryString += String.fromCharCode(uint8Array[i]);
    }
    return btoa(binaryString);
}
function uint8ArrayToString(uint8Array: Uint8Array): string {
    let decoder = new TextDecoder('utf-8');
    return decoder.decode(uint8Array);
}


class Socket {
    ws: WebSocket = null!;
    url: string = "ws://14.103.154.32:7001/v1/ws";


    connect(u: string, t: string, call?: Function, ct = 1) {
        this.url = this.url + "?u=" + u + "&t=" + t + "&ct=" + ct;

        this.ws = new WebSocket(this.url);
        this.ws.binaryType = "arraybuffer";

        this.ws.onopen = () => {
            console.log("socket open");
            call && call();
            setInterval(() => {
                let data = new_world.v1.ClientMessage.create({
                    msg_id: uuidv4(),
                    type: "ping",
                    key: "",
                })
                this.sendMessage(data);
            }, 5 * 1000)
        }
        this.ws.onmessage = (e) => {
            let data = new_world.v1.ServerMessage.decode(new Uint8Array(e.data))
            this.emitMessage(data);
        }
        this.ws.onclose = () => {
            console.log("socket close");
        }
        this.ws.onerror = () => {

        }
    }
    sendMessage(data: protocol.new_world.v1.ClientMessage) {
        let buffer = new_world.v1.ClientMessage.encode(data).finish();
        this.ws.send(buffer);
    }
    emitMessage(data: protocol.new_world.v1.ServerMessage) {
        if (userinfo.getFishs().length == 0) {
            return;
        }
        // let fish = userinfo.getFishs()[0];
        console.log(data)
        if (data.type === FishMessageType.empty) {

        } else if (data.type === FishMessageType.dead) {
            let body = new_world.v1.FishDeadReply.decode(data.body);
            if (body.fish) {
                userinfo.removeFishByid(body.fish.id)
            }
            console.log(body)
        } else if (data.type === FishMessageType.attack_record) {

        } else if (data.type === FishMessageType.growth) {
            let body = new_world.v1.FishGrowthReply.decode(data.body);
            let fish = userinfo.getFish(body.fish_id.toString());
            fish.fish.asset_info.level = body.growth_level+1;
            if (fish) {
                userinfo.updateFish(fish)
            }
            console.log(body)
        } else if (data.type === FishMessageType.earn) {
            let body = new_world.v1.FishEarnReply.decode(data.body);
            console.log(body)
            let fish = userinfo.getFish(body.fish_id.toString());
            fish.fish.asset_info.money  = body.current_money
            if (fish) {
                userinfo.updateFish(fish)
            }
            console.log(body)
        } else if (data.type === FishMessageType.explore) {
            let body = new_world.v1.FishExploreReply.decode(data.body);
            let fish = userinfo.getFish(body.fish_id.toString());
            fish.fish.battle_info.energy = body.current_energy;
            fish.fish.battle_info.max_energy = body.max_energy;
            if (fish) {
                userinfo.updateFish(fish)
            }
            console.log(body)
        } else if (data.type === FishMessageType.recover) {
            let body = new_world.v1.FishRecoverReply.decode(data.body);
            let fish = userinfo.getFish(body.fish_id.toString());
            fish.fish.battle_info.heal = body.current_hp;
            fish.fish.battle_info.max_heal = body.max_hp;
            if (fish) {
                userinfo.updateFish(fish)
            }
            console.log(body)
        } else if (data.type === FishMessageType.energy_recover) {
            let body = new_world.v1.FishEnergyRecoverReply.decode(data.body);
            let fish = userinfo.getFish(body.fish_id.toString());
            fish.fish.battle_info.energy = body.current_energy;
            fish.fish.battle_info.max_energy = body.max_energy;
            if (fish) {
                userinfo.updateFish(fish)
            }
            console.log(body)
        } else if (data.type === FishMessageType.stay) {
            let body = new_world.v1.FishStayReply.decode(data.body);
            console.log(body)
        }



    }

    send() {
        // let data = new_world.v1.ClientMessage.create({
        //     type: new_world.v1.ClientMessage.Type.HEARTBEAT,
        //     heartbeat: new_world.v1.Heartbeat.create()
        // })
    }
    closeconnect() {
        this.ws.close();
    }
}
export const socket = new Socket();